package com.itheima.easy.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.itheima.easy.annotation.DataScope;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.dto.RefundVoucherDto;
import com.itheima.easy.dto.RetreatDto;
import com.itheima.easy.dto.RetreatReqDto;
import com.itheima.easy.entity.Retreat;
import com.itheima.easy.service.RetreatService;
import com.itheima.easy.service.UserService;
import com.itheima.easy.utils.UserThreadLocal;
import com.itheima.easy.vo.DataScopeDto;
import com.itheima.easy.vo.SubjectVo;
import com.itheima.easy.vo.retreat.RetreatVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/elder")
@Api(tags = "退住")
public class RetreatController {

    @Autowired
    private RetreatService retreatService;

    @PostMapping("/create")
    @ApiOperation(value = "申请退住", notes = "传入退住对象")
    public AjaxResult createRetreat(
            @RequestBody @ApiParam(value = "退住对象", required = true) Retreat retreatDto) {
        return retreatService.createRetreat(retreatDto);
    }

    @GetMapping
    @ApiOperation(value = "退住表单查询")
    public AjaxResult<RetreatVo> getRetreat(
            @RequestParam @ApiParam(value = "退住编码") String retreatCode,
            @RequestParam @ApiParam(value = "处理人ID") String assigneeId,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus) {
        return retreatService.getRetreat(retreatCode, assigneeId, flowStatus);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交")
    public AjaxResult submitRetreat(
            @RequestBody @ApiParam(value = "退住对象", required = true) RetreatDto retreatDto) {
        return retreatService.submitRetreat(retreatDto);
    }

    @PutMapping
    @ApiOperation(value = "驳回")
    public AjaxResult disapprove(
            @RequestParam @ApiParam(value = "退住编码") String retreatCode,
            @RequestParam @ApiParam(value = "驳回消息") String message) {
        return retreatService.disapprove(retreatCode, message);
    }

    @PutMapping("/reject")
    @ApiOperation(value = "审核拒绝")
    public AjaxResult auditReject(
            @RequestParam @ApiParam(value = "退退住编码") String retreatCode,
            @RequestParam @ApiParam(value = "拒绝原因") String reject) {
        return retreatService.auditReject(retreatCode, reject);
    }

    @PutMapping("/revocation")
    @ApiOperation(value = "撤回")
    public AjaxResult revocation(
            @RequestParam @ApiParam(value = "退住编码") String retreatCode,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus) {
        return retreatService.revocation(retreatCode, flowStatus);
    }

    @PutMapping("/cancel")
    @ApiOperation(value = "撤销")
    public AjaxResult cancel(@RequestParam @ApiParam(value = "退住编码") String retreatCode){
        return retreatService.cancel(retreatCode);
    }

    @Autowired
    private UserService userService;

    /*@ApiOperation(value = "退住管理", notes = "退住管理列表页")
    @GetMapping("/selectByPage")
//    @DataScope(deptAlias = "d",userAlias = "u")
    public AjaxResult selectByPage(
            @RequestParam(required = false,defaultValue = "") String sqlDataScope,
            @ApiParam(value = "退住编码") @RequestParam(required = false) String retreatCode,
            @ApiParam(value = "姓名") @RequestParam(required = false) String name,
            @ApiParam(value = "身份证号") @RequestParam(required = false) String idCardNo,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Long startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Long endTime,
            @ApiParam(value = "页码") @RequestParam(required = true,defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量") @RequestParam(required = true,defaultValue = "10") Integer pageSize
    ) {
        String subject = UserThreadLocal.getSubject();
        SubjectVo subjectVo = JSONUtil.toBean(subject, SubjectVo.class);
        String deptNo = subjectVo.getDeptNo();
        Long userId = subjectVo.getId();

        LocalDateTime start = null;
        LocalDateTime end = null;
        if(startTime != null && endTime != null){
            start = LocalDateTimeUtil.of(startTime);
            end = LocalDateTimeUtil.of(endTime);
        }

        return retreatService.selectByPage(sqlDataScope,retreatCode, name, idCardNo, start, end, pageNum, pageSize,deptNo,userId);
    }*/

    @ApiOperation(value = "退住管理", notes = "退住管理列表页")
    @PostMapping("/selectByPage")
    @DataScope(deptAlias = "d",userAlias = "u")
    public AjaxResult selectByPage(@RequestBody RetreatReqDto retreatReqDto) {
        return retreatService.selectByPage(retreatReqDto);
    }
}
